package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Spec;
import com.changgou.service.SpecService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 规格信息控制层
 *
 * @author gaofeng
 * @date 2025/4/20 10:51
 */
@Api(value = "SpecController")
@RestController
@RequestMapping("/spec")
@CrossOrigin
public class SpecController {
    @Resource
    private SpecService specService;

    /**
     * Spec分页条件搜索实现
     *
     * @param spec 查询条件
     * @param page 当前页
     * @param size 每页显示条数
     * @return 规格信息分页结果
     */
    @ApiOperation(value = "Spec条件分页查询", notes = "分页条件查询Spec方法详情", tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Spec对象", value = "传入JSON数据", required = false) Spec spec, @PathVariable int page, @PathVariable int size) {
        PageInfo<Spec> pageInfo = specService.findPage(spec, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * Spec分页搜索实现
     *
     * @param page 当前页
     * @param size 每页显示多少条
     * @return 规格信息分页结果
     */
    @ApiOperation(value = "Spec分页查询", notes = "分页查询Spec方法详情", tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Spec> pageInfo = specService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param spec 查询条件
     * @return 规格信息集合
     */
    @ApiOperation(value = "Spec条件查询", notes = "条件查询Spec方法详情", tags = {"SpecController"})
    @PostMapping(value = "/search")
    public Result<List<Spec>> findList(@RequestBody(required = false) @ApiParam(name = "Spec对象", value = "传入JSON数据") Spec spec) {
        List<Spec> list = specService.findList(spec);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @ApiOperation(value = "Spec根据ID删除", notes = "根据ID删除Spec方法详情", tags = {"SpecController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        specService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改Spec数据
     *
     * @param spec 规格信息
     * @param id   主键
     * @return 修改结果
     */
    @ApiOperation(value = "Spec根据ID修改", notes = "根据ID修改Spec方法详情", tags = {"SpecController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody @ApiParam(name = "Spec对象", value = "传入JSON数据") Spec spec, @PathVariable Integer id) {
        spec.setId(id);
        specService.update(spec);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 新增Spec数据
     *
     * @param spec spec规格信息
     * @return 添加结果
     */
    @ApiOperation(value = "Spec添加", notes = "添加Spec方法详情", tags = {"SpecController"})
    @PostMapping(value = "/add")
    public Result add(@RequestBody @ApiParam(name = "Spec对象", value = "传入JSON数据", required = true) Spec spec) {
        specService.add(spec);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 根据ID查询Spec数据
     *
     * @param id 主键
     * @return 规格信息
     */
    @ApiOperation(value = "Spec根据ID查询", notes = "根据ID查询Spec方法详情", tags = {"SpecController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/findById/{id}")
    public Result<Spec> findById(@PathVariable Integer id) {
        Spec spec = specService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", spec);
    }

    /**
     * 查询Spec全部数据
     *
     * @return 规格信息集合
     */
    @ApiOperation(value = "查询所有Spec", notes = "查询所Spec有方法详情", tags = {"SpecController"})
    @GetMapping(value = "/findAll")
    public Result<List<Spec>> findAll() {
        List<Spec> list = specService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据categoryId查询规格信息
     *
     * @return com.changgou.entity.Result<java.util.List < com.changgou.goods.pojo.Spec>>
     */
    @ApiOperation(value = "根据categoryId查询规格信息", notes = "根据categoryId查询规格信息", tags = {"SpecController"})
    @GetMapping("/findByCategoryId/{categoryId}")
    public Result<List<Spec>> findByCategoryId(@PathVariable(value = "categoryId") Integer categoryId) {
        return new Result<>(true, StatusCode.OK, "查询成功", specService.findByCategoryId(categoryId));
    }
}
